package com.lbd.system.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.User;
import com.lbd.system.bean.UserMoneyStatistics;
import com.lbd.system.controller.vo.UserVo;
import com.lbd.system.service.UserMoneyStatisticsService;
import com.lbd.system.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by WangYajie on 2016/9/12.
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;
    @Autowired
    UserMoneyStatisticsService userMoneyStatisticsService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, UserVo user) {
        return new Result(ResultState.SUCCESS, "查询成功", userService.findByPageConditionJpql(page, rows, user));
    }


    /**
     * 禁用用户
     *
     * @param id 用户【表情】
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public Result disableUser(String id) {
        userService.disableUser(id);
        return new Result(ResultState.SUCCESS, "禁用成功");
    }

    /**
     * 启用用用户
     *
     * @param id 用户【表情】
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enableUser(String id) {
        userService.enableUser(id);
        return new Result(ResultState.SUCCESS, "启用成功");
    }

    /**
     * 重置密码
     *
     * @param id 用户【表情】
     * @return
     */
    @RequestMapping(value = "/rest/password", method = RequestMethod.POST)
    public Result restPassword(String id) {
        userService.restPassword(id);
        return new Result(ResultState.SUCCESS, "重置成功");
    }

    /**
     * 查看用户详情
     *
     * @param id 用户【表情】
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Result detail(String id) {
        User user = userService.findById(id);
        return new Result(ResultState.SUCCESS, "获取成功", user);
    }

    /**
     * 查询用户资金统计信息
     *
     * @param page 第几页
     * @param rows 一页多少条
     * @param user 查询条件
     * @return
     */
    @RequestMapping(value = "/fundStatistics", method = RequestMethod.GET)
    public Result fundStatistics(int page, int rows, UserVo user) {
        Page<User> userPage = userService.findByPageConditionJpql(page, rows, user);
        List<User> content = userPage.getContent();
        if (CollectionUtils.isNotEmpty(content)) {
            for (int i = 0; i < content.size(); i++) {
                UserMoneyStatistics userMoneyStatistics = userMoneyStatisticsService.findByUserId(content.get(i).getId());
                content.get(i).setAmountStatistics(userMoneyStatistics);
            }
        }
        return new Result(ResultState.SUCCESS, "查询成功", userPage);
    }

}
